//
//  SwitchClientTypeRequestHandler.m
//  Action
//
//  Created by 冯鸿杰 on 2017/6/27.
//  Copyright © 2017年 Mob. All rights reserved.
//

#import "PurifyWebPageRequestHandler.h"
#import <MobileCoreServices/MobileCoreServices.h>

@implementation PurifyWebPageRequestHandler

- (void)beginRequestWithExtensionContext:(NSExtensionContext *)context
{
    __weak typeof(self) weakSelf = self;
    NSExtensionItem *item = context.inputItems.firstObject;
    NSItemProvider *itemProvider = item.attachments.firstObject;
    if ([itemProvider hasItemConformingToTypeIdentifier:(NSString *)kUTTypePropertyList])
    {
        [itemProvider loadItemForTypeIdentifier:(NSString *)kUTTypePropertyList
                                        options:nil
                              completionHandler:^(id<NSSecureCoding>  _Nullable item, NSError * _Null_unspecified error) {
                                  
                                  NSDictionary *jsData = item[NSExtensionJavaScriptPreprocessingResultsKey];
                                  NSMutableString *text = [jsData[@"content"] mutableCopy];
                                  
                                  NSLog(@"----- raw text = %@", text);
                                  
                                  //去掉script
                                  NSRegularExpression *regExp1 = [[NSRegularExpression alloc] initWithPattern:@"<script[^>]*?>[\\s\\S]+?</script[^>]*?>" options:NSRegularExpressionCaseInsensitive error:nil];
                                  [regExp1 replaceMatchesInString:text options:NSMatchingReportProgress range:NSMakeRange(0, text.length) withTemplate:@""];
                                  
                                  //去掉样式
                                  NSRegularExpression *regExp2 = [[NSRegularExpression alloc] initWithPattern:@"<style[^>]*?>[\\s\\S]+?</style[^>]*?>" options:NSRegularExpressionCaseInsensitive error:nil];
                                  [regExp2 replaceMatchesInString:text options:NSMatchingReportProgress range:NSMakeRange(0, text.length) withTemplate:@""];
                                  
                                  //去掉noscript
                                  NSRegularExpression *regExp3 = [[NSRegularExpression alloc] initWithPattern:@"<noscript[^>]*?>[\\s\\S]+?</noscript[^>]*?>" options:NSRegularExpressionCaseInsensitive error:nil];
                                  [regExp3 replaceMatchesInString:text options:NSMatchingReportProgress range:NSMakeRange(0, text.length) withTemplate:@""];
                                  
                                  //去掉标签
                                  NSRegularExpression *regExp4 = [[NSRegularExpression alloc] initWithPattern:@"<[^>]+?>" options:NSRegularExpressionCaseInsensitive error:nil];
                                  [regExp4 replaceMatchesInString:text options:NSMatchingReportProgress range:NSMakeRange(0, text.length) withTemplate:@""];
                                  
                                  NSLog(@"----- purify text = %@", text);
                                  
                                  //必须设置NSExtensionJavaScriptFinalizeArgumentKey，否则无法返回JS
                                  NSDictionary *resultsDictionary = @{ NSExtensionJavaScriptFinalizeArgumentKey: @{@"content" : text} };
                                  NSItemProvider *resultsProvider = [[NSItemProvider alloc] initWithItem:resultsDictionary typeIdentifier:(NSString *)kUTTypePropertyList];
                                  NSExtensionItem *resultsItem = [[NSExtensionItem alloc] init];
                                  resultsItem.attachments = @[resultsProvider];
                                  [context completeRequestReturningItems:@[resultsItem] completionHandler:nil];
                              }];
    }
}

@end
